<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>发布资讯</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" type="text/css" th:href="@{/js/layui/css/layui.css}" media="all">
    <style>
        .childrenBody{
            padding: 30px;
        }
    </style>
</head>
<body class="childrenBody">
<form class="layui-form html_save" lay-filter="form">
    <div class="layui-form-item">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <input class="layui-input" name="content" lay-verify="content" placeholder="请输入内容" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <input class="layui-input" name="imgs" placeholder="上传图片" type="text">
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test1">上传图片</button>
            <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                预览图：
                <div class="layui-upload-list" id="demo1"></div>
            </blockquote>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">视频</label>
        <div class="layui-input-block">
            <input class="layui-input" name="videos" placeholder="上传视频" type="text">
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test2">上传视频</button>
            <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                预览图：
                <div class="layui-upload-list" id="demo2"></div>
            </blockquote>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">位置信息</label>
        <div class="layui-input-block">
            <input class="layui-input" name="location" lay-verify="location" placeholder="请输入位置信息" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveContainer">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script th:src="@{/js/layui/layui.js}"></script>

<script type="text/javascript" th:inline="javascript">
    layui.use(['table', 'form', "layer", 'upload'], function () {

        // 操作对象
        var form = layui.form
            , layer = layui.layer
            , upload = layui.upload
            , $ = layui.jquery;

        var imgList = [];
        var videoList = [];
        //执行上传
        upload.render({
            elem: '#test1'
            ,url: [[${basePath}]] + '/upload/oss'
            ,accept: 'file'
            ,before: function(obj){
                obj.preview(function(index, file, result){
                    $('#demo1').append('<img style="width: 50px;height: 50px;" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            , done: function (res) {
                $("input[name='imgs']").val('' + res.data);
                imgList.push(res.data);
            }
        });
        upload.render({
            elem: '#test2'
            ,url: [[${basePath}]] + '/upload/oss'
            ,accept: 'file'
            ,before: function(obj){
                obj.preview(function(index, file, result){
                    $('#demo2').append('<video style="width: 50px;height: 50px;" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            , done: function (res) {
                $("input[name='videos']").val('' + res.data);
                videoList.push(res.data);
            }
        });

        //监听提交
        form.on('submit(saveContainer)', function (data) {
            $.ajax({
                url: [[${basePath}]] + "/trend/public/release",
                data: {
                    content: data.field.content,
                    imgs: JSON.stringify(imgList),
                    videos: JSON.stringify(videoList),
                    location: data.field.location
                },
                type: "post",
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.href = [[${basePath}]] + "/trend/official/index";
                            parent.layer.close(index);
                        }, 1500);
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });
            return false;
        });

    });
</script>
</body>
</html>